﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Client
{
    public partial class LoginForm : System.Windows.Forms.Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private bool isLogin = false;

        // LogIn Dialog 를 화면에 표시
        public new DialogResult ShowDialog() 
        {
            base.ShowDialog();
            if (isLogin)
            {
                return DialogResult.Yes;
            }
            else
            {
                return DialogResult.No;
            }
        }

        private void AutoLogin()
        {
            bool isAutoLogin = (bool)RegistryAdmin.GetRegistryKey(RegistryAdmin.KEY_NAME.SAVE_ID);
            if (isAutoLogin == false)
            {
                SavedUserData sud = new SavedUserData(txtID.Text, txtPswd.Text);
                RegistryAdmin.SetRegistryKey(RegistryAdmin.KEY_NAME.SAVE_ID, sud);
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            CCL.SocketMessage.Request.Login packet = new CCL.SocketMessage.Request.Login(txtID.Text, txtPswd.Text);
            CCL.SocketMessage.Message message = CCL.SocketMessage.Message.MessageRequestBuilder(packet);
            byte[] msg = message.GetByte();

            /*
            string str = Connector.RecvMsg();
            if (null == str)
            {
                // Connector failed
                // 서버와의 연결이 안되어 있는 상태
                MessageBox.Show("서버와 연결이 되어있지 않습니다.");
                return;
            }

            string recvMsg = Connector.RecvMsg();*/

            CCL.SocketMessage.Message loginMsg = MainForm.connector.Execute(msg);
            CCL.SocketMessage.Response.Login loginResponse = (CCL.SocketMessage.Response.Login)loginMsg.Body;
            
            // 로그인 확인
            if(loginResponse.Result == CCL.SocketMessage.Constant.Result.Success)
            {
                // 유저정보를 입력함.

                MainForm.userData = loginResponse.UserInfo;
                isLogin = true;

                CCL.SocketMessage.Request.GetProjectList reqPacket = new CCL.SocketMessage.Request.GetProjectList(MainForm.userData.UserID);
                CCL.SocketMessage.Message plMessage = CCL.SocketMessage.Message.MessageRequestBuilder(reqPacket);
                CCL.SocketMessage.Message reqMsg = MainForm.connector.Execute(plMessage.GetByte());
                CCL.SocketMessage.Response.GetProjectList plResponse = (CCL.SocketMessage.Response.GetProjectList)reqMsg.Body;
                if (plResponse.Result == CCL.SocketMessage.Constant.Result.Success)
                {
                     MainForm.projList = plResponse.ProjectList;
                }

                // 자동 로그인을 체크했을 경우
                // 자동 로그인 레지스트리를 설정해줌.
                if (cbAutoLogin.Checked == true)
                {
                    AutoLogin();
                }
                this.Close();
            }
            else
            {
                MessageBox.Show(CCL.Utility.StringUtility.ConvertToStr(loginResponse.ErrorMessage));
                isLogin = false;
            }

        }

        private void btnJoin_Click(object sender, EventArgs e)
        {
            CreateAccountForm joinForm = new CreateAccountForm();
            joinForm.Show();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void cbAutoLogin_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void txtPswd_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtID_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}
